import { mapGetters, mapMutations } from 'vuex'
import playMode from '@/config/playMode'
import { shuffle } from '@/utils'

export default {
  computed: {
    iconMode() {
      return this.mode === playMode.sequence ? 'icon-liebiaoxunhuan' : this.mode === playMode.loop ? 'icon-danquxunhuan' : 'icon-suijibofang'
    },
    playModeTxt() {
      return this.mode === 0 ? '列表循环' : this.mode === 1 ? '单曲循环' : '随机播放'
    },
    ...mapGetters('player', ['mode', 'playing', 'fullScreen', 'currentSong', 'playList', 'currentIndex', 'sequenceList'])
  },
  methods: {
    resetCurrentIndex(list) {
      let index = list.findIndex((item) => {
        return item.id === this.currentSong.id
      })
      this.setCurrentIndex(index)
    },
    changePalyMode() {
      let mode = (this.mode + 1) % 3
      this.setPalyMode(mode)

      let palyList = null
      if (mode === playMode.random){
        palyList = shuffle(this.sequenceList)
      } else {
        palyList = this.sequenceList
      }
      this.resetCurrentIndex(palyList)
      this.setPlayList(palyList)
      this.$toast(this.playModeTxt)
    },
    ...mapMutations('player', {
      setPlayList: 'SET_PLAYLIST',
      setPalyMode: 'SET_PALYMODE',
      setPalyingState: 'SET_PALYING_STATE',
      setCurrentIndex: 'SET_CURRENT_INDEX',
      clearPlayList: 'CLEAR_PLAYLIST',
    })
  }
}